package com.cheyun.netsalev3.tencenttim.profile.clipimagedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;

import java.io.File;


public class ClipImageLayout extends RelativeLayout {
	private ClipZoomImageView mZoomImageView;
	private ClipImageBorderView mClipImageView;
	private int  isrect = 0;
	private Context context;
	private int mHorizontalPadding = 0;// 框左右的边距，这里左右边距为0，为�?��屏幕宽度的正方形�?
	private int screeWidth;
	private int screeHeight;
	public ClipImageLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		screeWidth = display.getWidth();
		screeHeight = display.getHeight();
		mZoomImageView = new ClipZoomImageView(context,isrect);
		mClipImageView = new ClipImageBorderView(context,isrect);

		ViewGroup.LayoutParams lp = new LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);

		//this.addView(mZoomImageView, lp);
		this.addView(mClipImageView, lp);
		mZoomImageView.setLayoutParams(lp);
		this.addView(mZoomImageView);

				// 计算padding的px
		mHorizontalPadding = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources()
						.getDisplayMetrics());
		mZoomImageView.setHorizontalPadding(mHorizontalPadding,0);
		mClipImageView.setHorizontalPadding(mHorizontalPadding,0);
	}

	public void settype(Context context,int isrect){
		this.isrect = isrect;
		mZoomImageView = new ClipZoomImageView(context,isrect);
		mClipImageView = new ClipImageBorderView(context,isrect);

		ViewGroup.LayoutParams lp = new LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);

		this.addView(mZoomImageView, lp);
		this.addView(mClipImageView, lp);

		// 计算padding的px
		mHorizontalPadding = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources()
						.getDisplayMetrics());
		mZoomImageView.setHorizontalPadding(mHorizontalPadding,0);
		mClipImageView.setHorizontalPadding(mHorizontalPadding,0);

	}

	public void setImageDrawable(Drawable drawable) {
		mZoomImageView.setImageDrawable(drawable);
		//mZoomImageView.setAdjustViewBounds(true);
	}

	public void setImageBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float scaleWidth = ((float) screeWidth) / width;
		if(isrect == 20 || isrect == 21) {
			//长图，高度超过当前屏幕，按高度全屏进行缩放裁剪
			if (scaleWidth * height > screeHeight) {
				mClipImageView.setIsBigImg(true);
				//高度超过当前屏幕
				scaleWidth = ((float) screeHeight) / height;
				int mHorizontalPadding = (int) (screeWidth - width * scaleWidth) / 2;
				mClipImageView.setHorizontalPadding(mHorizontalPadding,0);
				mZoomImageView.setHorizontalPadding(mHorizontalPadding,0);
			}else{
				//高度小于当前屏幕
				int hh = getHeight();
				float scaleHeight = ((float)height/screeHeight);
				int mHorizontalPadding = (int)((1- scaleHeight)*screeWidth/2);
				mClipImageView.setHorizontalPadding(mHorizontalPadding,scaleHeight);
				mZoomImageView.setHorizontalPadding(mHorizontalPadding,scaleHeight);
			}
		}
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleWidth);
		Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

		mZoomImageView.setImageBitmap(newbm);
		mZoomImageView.setScaleType(ImageView.ScaleType.CENTER);
	}
	public void setImageBitmaptoPath(String path,int width,int height) {
		Picasso.with(context).load(new File(path)).into(mZoomImageView);
	}
	/**
	 * 对外公布设置边距的方�?单位为dp
	 * 
	 * @param mHorizontalPadding
	 */
	public void setHorizontalPadding(int mHorizontalPadding) {
		this.mHorizontalPadding = mHorizontalPadding;
	}

	/**
	 * 裁切图片
	 * 
	 * @return
	 */
	public Bitmap clip() {
		return mZoomImageView.clip();
	}
}
